package com.ksd.modules.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.course.entity.AdminCourseCode;
import com.ksd.modules.course.mapper.AdminCourseCodeMapper;
import com.ksd.modules.course.po.AdminCourseCodeSavePO;
import com.ksd.modules.course.po.AdminCourseCodeSearchPO;
import com.ksd.modules.course.service.IAdminCourseCodeService;
import com.ksd.modules.course.vo.AdminCourseCodeVo;
import com.ksd.utils.snow.SnowflakeIdWorker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminCourseCodeServiceImpl extends BaseServiceImpl<AdminCourseCodeMapper, AdminCourseCode> implements IAdminCourseCodeService {
    
    /**
     * 查询列表
     * @param adminCourseCodeSearchPO
     * @return
     */
    public Page<AdminCourseCodeVo> findAdminCourseCodes(AdminCourseCodeSearchPO adminCourseCodeSearchPO){
        Page<AdminCourseCode> page = new Page<>(adminCourseCodeSearchPO.getPageNo(),adminCourseCodeSearchPO.getPageSize());
        LambdaQueryWrapper<AdminCourseCode> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminCourseCode::getSystemId,SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.eq(Objects.nonNull(adminCourseCodeSearchPO.getOpid()),AdminCourseCode::getOpid,adminCourseCodeSearchPO.getOpid());
        lambdaQueryWrapper.eq(Objects.nonNull(adminCourseCodeSearchPO.getMark()),AdminCourseCode::getMark,adminCourseCodeSearchPO.getMark());
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(adminCourseCodeSearchPO.getCode()), AdminCourseCode::getCode,adminCourseCodeSearchPO.getCode());
        lambdaQueryWrapper.orderByDesc(AdminCourseCode::getCreateTime);
        Page<AdminCourseCode> adminCoursePage = this.page(page, lambdaQueryWrapper);
        Page<AdminCourseCodeVo> adminCourseVoPage = tranferPageBo(adminCoursePage, AdminCourseCodeVo.class);
        return adminCourseVoPage;
    }


    /**
     * 创建兑换码
     * @param adminCourseCodeSavePO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean createBatchCode(AdminCourseCodeSavePO adminCourseCodeSavePO){
        AdminCourseCode adminUserWalletCode= tranferBo(adminCourseCodeSavePO, AdminCourseCode.class);
        adminUserWalletCode.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminUserWalletCode.setSystemName(SystemIdThrealLocal.get().getSystemName());
        adminUserWalletCode.setCode(new SnowflakeIdWorker(9,8).nextId()+"");
        adminUserWalletCode.setMark(0);
        this.saveOrUpdate(adminUserWalletCode);
        return true;
    }

    /**
     * 单个物理删除
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean removeAdminCourseCodeById(Long id){
        boolean b = this.removeById(id);
        return b;
    }


    /**
     * 批量物理删除
     * @param batchIdsPO
     * @return
     */
    @Transactional(rollbackFor=Exception.class)
    public Boolean batchRemovesAdminCourseCode(BatchIdsPO batchIdsPO){
        if (StringUtils.isEmpty(batchIdsPO.getIds())) {
            return false;
        }
        // 把文章分类进行恢复
        List<Long> ids = Arrays.stream(batchIdsPO.getIds().split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean b = this.removeByIds(ids);
        return b;
    }


    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public boolean updateStatus(Long id,String column,Integer value){
        boolean b = super.updateStatus(id,column,value);
        return b;
    }

    /**
     * 修改状态
     * @param id
     * @param column
     * @param value
     * @return
     */
    @Override
    public boolean updateStatusStr(Long id,String column,String value){
        boolean b = super.updateStatusStr(id,column,value);
        return b;
    }


}
